<html>
<head><meta charset="utf-8"><title>chunks_exact remainder gripes · t-libs · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/index.html">t-libs</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/chunks_exact.20remainder.20gripes.html">chunks_exact remainder gripes</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="245389953"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/chunks_exact%20remainder%20gripes/near/245389953" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/chunks_exact.20remainder.20gripes.html#245389953">(Jul 09 2021 at 01:05)</a>:</h4>
<p>The <code>chunks_exact</code> functions (anything that matches <code>r?chunks_exact(_mut)?</code>) are all really pretty awkward to use correctly in the (common, IMO) case where you at least care <em>somewhat</em> about the remainder. Even if it's just to know it's len (but often its to process it differently).</p>
<p>It requires something like:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="n">iter</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">slice</span><span class="p">.</span><span class="n">chunks_exact</span><span class="p">(</span><span class="n">N</span><span class="p">);</span><span class="w"></span>
<span class="kd">let</span><span class="w"> </span><span class="n">rem</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">iter</span><span class="p">.</span><span class="n">remainder</span><span class="p">();</span><span class="w"></span>
<span class="k">for</span><span class="w"> </span><span class="n">chunk</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="n">iter</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="c1">// use chunk</span>
<span class="p">}</span><span class="w"></span>
<span class="c1">// use rem</span>
</code></pre></div>
<p>Is there anything that can/should be done to improve this? If not, can we avoid the same issues in the upcoming <code>array_chunks </code>?</p>



<a name="245390172"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/chunks_exact%20remainder%20gripes/near/245390172" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> rpjohnst <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/chunks_exact.20remainder.20gripes.html#245390172">(Jul 09 2021 at 01:10)</a>:</h4>
<p>if you could match on the return value you could write <code>let (iter, rem) = slice.chunks_exact(N); for chunk in iter { .. }</code>, which is maybe nicer but still pretty awkward compared to the case where you ignore the remainder: <code>for chunk in slice.chunks_exact(N) { .. }</code></p>



<a name="245391342"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/chunks_exact%20remainder%20gripes/near/245391342" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/chunks_exact.20remainder.20gripes.html#245391342">(Jul 09 2021 at 01:35)</a>:</h4>
<p><span class="user-mention silent" data-user-id="117495">rpjohnst</span> <a href="#narrow/stream/219381-t-libs/topic/chunks_exact.20remainder.20gripes/near/245390172">said</a>:</p>
<blockquote>
<p>if you could match on the return value you could write <code>let (iter, rem) = slice.chunks_exact(N); for chunk in iter { .. }</code></p>
</blockquote>
<p>That would require chunks_exact returning a tuple, no?</p>



<a name="245409481"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/chunks_exact%20remainder%20gripes/near/245409481" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> The 8472 <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/chunks_exact.20remainder.20gripes.html#245409481">(Jul 09 2021 at 07:24)</a>:</h4>
<p>adding a <code>.self_and_remainder()</code> to the adapter should be feasible</p>



<a name="245488142"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/chunks_exact%20remainder%20gripes/near/245488142" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> rpjohnst <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/chunks_exact.20remainder.20gripes.html#245488142">(Jul 09 2021 at 19:41)</a>:</h4>
<p><span class="user-mention silent" data-user-id="209168">Thom Chiovoloni</span> <a href="#narrow/stream/219381-t-libs/topic/chunks_exact.20remainder.20gripes/near/245391342">said</a>:</p>
<blockquote>
<p>That would require chunks_exact returning a tuple, no?</p>
</blockquote>
<p>yeah, or a struct with public fields- just speaking generally about matching vs .remainder()</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>